package com.gzq.servlets;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.SingleSelectionModel;

/**
 * @author GZQ 演示servlet的生命周期。
 */
@WebServlet(value = "/lifecycle")
public class LifeCycleServlet implements Servlet {
	private static final String NAME = "LifeCycleServlet.";
	private ServletConfig config;

	public LifeCycleServlet() {
		System.out.println(NAME + "构造器 LifeCycleServlet()，hash值：" + this.hashCode());
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		this.config = config;
		System.out.println(NAME + "初始化 init(ServletConfig config)，hash值：" + this.hashCode());
	}

	@Override
	public ServletConfig getServletConfig() {
		return config;
	}

	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		System.out.println(NAME + "服务 service(ServletRequest req, ServletResponse res)，hash值：" + this.hashCode());
		res.getWriter().write("LifeCycleServlet");
	}

	@Override
	public String getServletInfo() {
		return "该方法是返回一个关于当前servlet的作者，版本，版权等信息的字符串";
	}

	@Override
	public void destroy() {
		System.out.println(NAME + "服务 destroy()，hash值：" + this.hashCode());
	}

}
